package com.ywb.ex04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Task2 {

    private JFrame frame;
    private JTextField textField;
    int fare=0;

    JLabel lblNewLabel_7;
    JLabel lblNewLabel_8;
    JLabel lblNewLabel_9;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new Task2().frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public Task2() {
        initialize();
    }


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);

        JLabel label = new JLabel("\u8F66\u7968\u6570\u91CF\uFF1A");
        label.setFont(new Font("宋体", Font.PLAIN, 20));
        panel.add(label);

        textField = new JTextField("0");
        textField.setFont(new Font("宋体", Font.PLAIN, 18));
        panel.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("\u5F20");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        panel.add(lblNewLabel);

        JPanel panel_2 = new JPanel();
        frame.getContentPane().add(panel_2, BorderLayout.SOUTH);

        JButton start = new JButton("\u5F00\u59CB\u62A2\u7968");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_startButton_actionPerformed(e);
            }
        });
        start.setFont(new Font("宋体", Font.PLAIN, 18));
        panel_2.add(start);

        JButton reset = new JButton("\u91CD\u7F6E");
        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_resetButton_actionPerformed(e);
            }
        });
        reset.setFont(new Font("宋体", Font.PLAIN, 18));
        panel_2.add(reset);

        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(null);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setBounds(60, 40, 120, 120);
        panel_1.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("");
        lblNewLabel_2.setBounds(240, 40, 120, 120);
        panel_1.add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("");
        lblNewLabel_3.setBounds(420, 40, 120, 120);
        panel_1.add(lblNewLabel_3);


        ImageIcon icon_1=new ImageIcon("src/main/java/research04/img/1.jpg");
        icon_1.setImage(icon_1.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT));
        lblNewLabel_1.setIcon(icon_1);

        ImageIcon icon_2=new ImageIcon("src/main/java/research04/img/2.jpg");
        icon_2.setImage(icon_2.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT));
        lblNewLabel_2.setIcon(icon_2);

        ImageIcon icon_3=new ImageIcon("src/main/resources/img/3.jpg");
        icon_3.setImage(icon_3.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT));
        lblNewLabel_3.setIcon(icon_3);

        JLabel lblNewLabel_4 = new JLabel("\u5C0F\u5F3A");
        lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_4.setBounds(60, 180, 30, 15);
        panel_1.add(lblNewLabel_4);

        JLabel lblNewLabel_5 = new JLabel("\u5C0F\u7EA2");
        lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_5.setBounds(240, 180, 30, 15);
        panel_1.add(lblNewLabel_5);

        JLabel lblNewLabel_6 = new JLabel("\u5C0F\u521A");
        lblNewLabel_6.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_6.setBounds(420, 180, 30, 15);
        panel_1.add(lblNewLabel_6);

        lblNewLabel_7 = new JLabel("0");
        lblNewLabel_7.setBounds(60, 205, 54, 15);
        panel_1.add(lblNewLabel_7);

        lblNewLabel_8 = new JLabel("0");
        lblNewLabel_8.setBounds(240, 205, 54, 15);
        panel_1.add(lblNewLabel_8);

        lblNewLabel_9 = new JLabel("0");
        lblNewLabel_9.setBounds(420, 205, 54, 15);
        panel_1.add(lblNewLabel_9);

        JLabel lblNewLabel_10 = new JLabel("\u5F20");
        lblNewLabel_10.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_10.setBounds(100, 205, 15, 15);
        panel_1.add(lblNewLabel_10);

        JLabel lblNewLabel_11 = new JLabel("\u5F20");
        lblNewLabel_11.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_11.setBounds(280, 205, 15, 15);
        panel_1.add(lblNewLabel_11);

        JLabel lblNewLabel_12 = new JLabel("\u5F20");
        lblNewLabel_12.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_12.setBounds(460, 205, 15, 15);
        panel_1.add(lblNewLabel_12);
    }

    protected void do_resetButton_actionPerformed(ActionEvent e) {
        fare=0;
        lblNewLabel_7.setText("0");
        lblNewLabel_8.setText("0");
        lblNewLabel_9.setText("0");
        textField.setText("0");
    }
    //开始抢票按钮
    protected void do_startButton_actionPerformed(ActionEvent e) {
        fare=0;
        lblNewLabel_7.setText("0");
        lblNewLabel_8.setText("0");
        lblNewLabel_9.setText("0");
        textField.setEditable(false);

        fare=Integer.valueOf(textField.getText());

        MyThread t=new MyThread(fare,textField,lblNewLabel_7,lblNewLabel_8,lblNewLabel_9);
        Thread t1=new Thread(t);
        Thread t2=new Thread(t);
        Thread t3=new Thread(t);
        t1.setName("一");
        t2.setName("二");
        t3.setName("三");

        t1.start();
        t2.start();
        t3.start();

    }




}

class MyThread implements Runnable{
    int flag_1=0,flag_2=0,flag_3=0;
    boolean flag=false;

    int fare;
    JTextField textField;
    JLabel lblNewLabel_7;
    JLabel lblNewLabel_8;
    JLabel lblNewLabel_9;

    public MyThread(int fare, JTextField textField, JLabel lblNewLabel_7, JLabel lblNewLabel_8, JLabel lblNewLabel_9) {
        this.fare = fare;
        this.textField = textField;
        this.lblNewLabel_7 = lblNewLabel_7;
        this.lblNewLabel_8 = lblNewLabel_8;
        this.lblNewLabel_9 = lblNewLabel_9;
    }
    //线程同步
    public synchronized void do_Thread(){
        if(fare>0){
            fare--;
            if(Thread.currentThread().getName().equals("一")){
                flag_1++;
                lblNewLabel_7.setText(String.valueOf(flag_1));
            }else if(Thread.currentThread().getName().equals("二")){
                flag_2++;
                lblNewLabel_8.setText(String.valueOf(flag_2));
            }else if(Thread.currentThread().getName().equals("三")){
                flag_3++;
                lblNewLabel_9.setText(String.valueOf(flag_3));
            }
        }
    }

    public void run() {
        while(!flag){
            try{
                Thread.sleep(200);
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }
            do_Thread();
            if(fare == 0){
                textField.setEditable(true);
                flag = true;
            }
        }

    }

}
